package id.mankel.commons.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.Map;
import java.util.Set;

public class MyVelocityTemplateEngine extends VelocityTemplateEngine {

    private static final String SUFFIX_JAVA = ".java";
    private static final String SUFFIX_VUE = ".vue";

    private static final String NONE_BIZ_FIELD_KEY = "noneBizFields";
    private static final Set<String> NONE_BIZ_FIELDS = Set.of(
            "id", "creator_id", "created_time", "reviser_id", "revised_time"
    );

    @Override
    protected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
        ConfigBuilder config = (ConfigBuilder) objectMap.get("config");
        PackageConfig packageConfig = config.getPackageConfig();
        objectMap.putIfAbsent("voPackage", packageConfig.getParent() + ".vo");
        objectMap.putIfAbsent(NONE_BIZ_FIELD_KEY, NONE_BIZ_FIELDS);

        // vue
        String moduleName = packageConfig.getModuleName();
        String otherPath = getPathInfo(OutputFile.other);
        customFile.entrySet().stream().filter(e -> e.getKey().endsWith(SUFFIX_VUE)).forEach(e -> {
            String fileName = String.format((otherPath + File.separator + moduleName + File.separator + "%s"), e.getKey());
            outputFile(new File(fileName), objectMap, e.getValue());
        });

        // vo
        String voPath = config.getGlobalConfig().getOutputDir()
                + File.separator
                + packageConfig.joinPackage("vo").replaceAll("\\.", StringPool.BACK_SLASH + File.separator);
        customFile.entrySet().stream().filter(e -> e.getKey().endsWith(SUFFIX_JAVA)).forEach(e -> {
            String fileName = String.format((voPath + File.separator + "%s"), e.getKey());
            outputFile(new File(fileName), objectMap, e.getValue());
        });
    }

    @Override
    protected void outputService(@NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
        // IMpService.java
        String entityName = tableInfo.getEntityName();
//        String servicePath = getPathInfo(OutputFile.service);
//        if (StringUtils.isNotBlank(tableInfo.getServiceName()) && StringUtils.isNotBlank(servicePath)) {
//            getTemplateFilePath(TemplateConfig::getService).ifPresent(service -> {
//                String serviceFile = String.format((servicePath + File.separator + tableInfo.getServiceName() + suffixJavaOrKt()), entityName);
//                outputFile(new File(serviceFile), objectMap, service);
//            });
//        }
        // MpServiceImpl.java
        String serviceImplPath = getPathInfo(OutputFile.serviceImpl);
        if (StringUtils.isNotBlank(tableInfo.getServiceImplName()) && StringUtils.isNotBlank(serviceImplPath)) {
            getTemplateFilePath(TemplateConfig::getServiceImpl).ifPresent(serviceImpl -> {
                String implFile = String.format((serviceImplPath + File.separator + tableInfo.getServiceImplName() + suffixJavaOrKt()), entityName);
                outputFile(new File(implFile), objectMap, serviceImpl);
            });
        }
    }

}
